﻿using Couchbase;
using Enyim.Caching.Memcached;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Sportsbook.CouchbasePersistence.CouchbaseClients
{
    public abstract class Client
    {
        protected CouchbaseClient client;
        
        public  bool StoreJson<T>(StoreMode storeMode, string key, T value) where T : class
        {
            var json = JsonConvert.SerializeObject(value,new StringEnumConverter());
            return client.Store(storeMode, key, json);
        }
        public  T GetJson<T>(string key) where T : class, new()
        {
            var json = client.Get<string>(key);
            if (json == null) return new T();
            var obj = JsonConvert.DeserializeObject<T>(json, new StringEnumConverter());
            return obj;
        }
        public IView<T> GetView<T>(string designName, string viewName, bool shouldLookupDocById = false) where T : class
        {
            return client.GetView<T>(designName, viewName, shouldLookupDocById);
        }
    }
}
